﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SaleWebsite.Models;

namespace SaleWebsite.Controllers
{
    public class ProviderController : Controller
    {
        //
        // GET: /Provider/

        public ActionResult Index()
        {
            return View(Provider.ConvertAll(Provider.GetAll()));
        }
        public ActionResult insert(string name,string address,string phone,string id_cart,string email)
        {

            Provider item = new Provider() {Name=name,Address=address,Phone=phone,Id_cart=id_cart,Email=email };
            ulong id = item.Insert();
            item.Id = id;
            return PartialView("provider_row", item);


        }
        public JsonResult delete(ulong id)
        {
            Provider item = new Provider() { Id=id};
            if (item.Delete() > 0)
            {
                return Json(new { status = "success" });
            }
            else
            {
                return Json(new { status = "not" });
            }

        }
        public ActionResult Save_provider(string name, string address, string phone, string id_cart, string email,ulong id)
        {
            try
            {
                Provider item = new Provider() {Id=id,Name=name,Address=address,Phone=phone,Id_cart=id_cart,Email=email };
                if (item.Save() > 0)
                {
                    //return Json(new { error = "update Faile" });
                    return PartialView("provider_row", item);
                }
                else
                {
                    return Json(new { error = "update Faile" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { error = ex.Message });
            }
        }

    }
}
